<template>
  <div class="app-container">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="使能">
        <el-switch v-model="form.ntpserverenable"></el-switch>
      </el-form-item>
      <el-form-item label="ip" prop="ntpserverip">
        <el-col :xs="12" :sm="6" :md="6" :lg="4"  :xl="3">
          <el-input v-model="form.ntpserverip"></el-input>
        </el-col>
      </el-form-item>
      <el-form-item label="端口号" prop="ntpserverport">
        <el-col :xs="12" :sm="6" :md="6" :lg="4"  :xl="3">
          <el-input v-model="form.ntpserverport"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="setntpinfo">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
    export default {
        name: "ntpServer",
      data:function () {
        var regExp=this.BASE.validateItem
        var ipcheck = (rule, value, callback) => {
          if(value===""){
            callback(new Error('请输入IP'));
          }
          else if (!regExp.ip.test(value)) {
            callback(new Error('IP格式不正确'));
          }else {
            callback();
          }
        }
        var portcheck = (rule, value, callback) => {
          if(value===""){
            callback(new Error('请输入端口号'));
          }else if (!regExp.port.test(value)) {
            callback(new Error('端口号只能输入0 ~ 65535之间的数字'));
          }else {
            callback();
          }
        }
        return{
          rules: {
            ntpserverip: [
              { validator: ipcheck,required: true, trigger: 'blur' }
            ],
            ntpserverport: [
              { validator: portcheck,required: true, trigger: 'blur' }
            ]
          },
          form: {
            ntpserverip: '',
            ntpserverport: '',
            ntpserverenable:''
          }
        }
      },methods:{
        getntpinfo:function(){
          var _this=this
          _this.postAjax({
            method:'getntpinfo',
            params:{}
          }).then(function (res) {
            console.log(res);
            if(res.result==0){
              var data=res.params
              // debugger
              _this.form.ntpserverip=data.ntpserverip
              _this.form.ntpserverport=data.ntpserverport
              _this.form.ntpserverenable=data.ntpserverenable==1?true:false
            }else{
              _this.tips(res.errorInfo.detail,'error')
            }
          }).catch(function (err) {
            console.log(err);
          }).finally(function (fn) {
            console.log(fn);
          })
        },
        setntpinfo:function () {
          var _this=this
          this.$refs['form'].validate((valid) => {
            if (valid) {
              _this.loading=true
              _this.postAjax({
                method:'setntpinfo',
                params:{
                  ntpserverip:_this.form.ntpserverip,
                  ntpserverport:_this.form.ntpserverport*1,
                  ntpserverenable:_this.form.ntpserverenable?1:0
                }
              }).then(function (res) {
                console.log(res);
                if(res.result==0){
                  // debugger
                  _this.tips('保存成功','success')
                  // var data=res.params
                  // // debugger
                  // _this.form.ip=data.ip
                  // _this.form.gateway=data.gateway
                  // _this.form.mask=data.mask
                  // _this.form.dns1=data.dns1
                  // _this.form.dns2=data.dns2
                }else {
                  _this.tips(res.errorInfo.detail,'error')
                }
              }).catch(function (err) {
                console.log(err);
              }).finally(function (fn) {
                _this.loading=false
                console.log(fn);
              })
            }else{
              return false;
            }
          })
        }
      },mounted:function () {
        this.getntpinfo()
      }
    }
</script>

<style scoped>

</style>
